package com.malamute.core.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.malamute.core.hibernate.HibernateBaseDao;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author AceGuan gjybst@163.com
 * @description 树tag编写方法
 * @date 2012-2-5 下午08:52:18
 * @version v1.0
 */

public class TreeComponent extends Component {
	private String className;
	private HttpServletRequest req;
	private static Log log = LogFactory.getLog(TreeTag.class);

	/**
	 * 设置req
	 * 
	 * @param req
	 */
	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	/**
	 * 获取className
	 * 
	 * @return className
	 */

	public String getClassName() {
		return className;
	}

	/**
	 * 设置className
	 * 
	 * @param className
	 */
	public void setClassName(String className) {
		this.className = className;
	}

	/**
	 * @Title:TreeComponent
	 * @Description:TODO
	 * @param @param stack
	 */
	public TreeComponent(ValueStack stack) {
		super(stack);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param writer
	 * 
	 * @return
	 * 
	 * @see org.apache.struts2.components.Component#start(java.io.Writer)
	 */
	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			StringBuilder str = new StringBuilder();
			boolean isValid = true;

			if (isValid) {
				if (className.startsWith("%{") && className.endsWith("}")) {
					className = className.substring(2, className.length() - 1);
					isValid = className == null ? false : true;
				} else {
					isValid = false;
				}
			}

			try {
				Class treeNodeClass = Class.forName(className);

				WebApplicationContext wac = WebApplicationContextUtils
						.getRequiredWebApplicationContext(req.getSession()
								.getServletContext());

				for (String name : wac.getBeanDefinitionNames()) {
					log.debug("SpringBeanName:------------------" + name);
				}
				HibernateBaseDao hibernateDao = (HibernateBaseDao) wac
						.getBean("hibernateBaseDaoImpl");
				List list = hibernateDao.listForTree(treeNodeClass);
				for (java.util.Iterator it = list.iterator(); it.hasNext();) {
					str.append(it.next());
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			str.append("标签内容" + className);
			str.append("<ul id=\"tt\"><li> <span>Folder</span>  <ul><li><span>Sub Folder 1</span><ul><li><span><a href=\"#\">File 11</a></span></li> "
					+ "<li><span>File 12</span></li><li><span>File 13</span></li></ul></li><li> </li><li><span>File 3</span></li></ul>"
					+ "</li><li><span>File21</span></li></ul>");
			
			str.append("<script type=\"text/javascript\">$('#tt').tree();</script>");
			writer.write(str.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
